1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
|
import { Metadata } from "next";
import kairosImage from "../kairos.png";
import { createClient } from "@libsql/client";
import Link from 'next/link';
interface NewsData {
identifier: string;
type: string | null;
timestamp: number;
headline: string | null;
content: string;
url: string | null;
images: Array<{
image: string;
link: string | null;
}>;
en_headline: string | null;
en_content: string | null;
is_ai_summary: boolean | null;
}
export async function generateMetadata({
params,
searchParams,
}: {
params: Promise<{ gameName?: string }>;
searchParams: Promise<{ [key: string]: string | string[] | undefined }>;
}): Promise<Metadata> {
const resolvedParams = await params;
const resolvedSearchParams = await searchParams;
let gameName = resolvedParams.gameName || "news";
const postId = resolvedSearchParams.post as string | undefined;
const lang = resolvedSearchParams.lang as string | undefined;
const apiUrlBase = process.env.NEXT_PUBLIC_API_URL;
if (!postId) {
return {
title: `${gameName} News`,
description: `Browse the latest updates for ${gameName}`,
};
}
try {
let fetchUrl = `${apiUrlBase}/${gameName}_news.json`;
if (gameName === "news") {
fetchUrl = `${apiUrlBase}/news.json`;
}
const res = await fetch(fetchUrl);
if (!res.ok) throw new Error("Failed to fetch");
const data = await res.json();
const newsPosts = data.news_posts;
const matchingPost = newsPosts.find((news: any) => {
const contentHash =
news.content.split("").reduce((hash: number, char: string) => {
return (hash << 5) + hash + char.charCodeAt(0);
}, 5381) >>> 0;
const headlineHash =
(news.headline || "null")
.split("")
.reduce(
(hash: number, char: string) =>
(hash << 5) + hash + char.charCodeAt(0),
5381,
) >>> 0;
const newsId = `${news.identifier}-${news.timestamp}-${contentHash.toString(16)}-${headlineHash.toString(16)}`;
return newsId === postId;
});
if (!matchingPost) {
try {
const client = createClient({
url: process.env.SQLITE_DB!,
authToken: process.env.REMOTE_AUTH_TOKEN!,
});
const result = await client.execute({
sql: `SELECT
news_id, date, identifier, type, timestamp,
headline, content, url, is_ai_summary,
en_headline, en_content
FROM news
WHERE news_id = ?`,
args: [postId],
});
if (result.rows.length === 0) {
return { title: "Post not found" };
}
const row = result.rows[0];
const imagesResult = await client.execute({
sql: `SELECT image_url, link_url FROM news_images WHERE news_id = ?`,
args: [postId],
});
const images = imagesResult.rows.map((img) => ({
image: img.image_url,
link: img.link_url,
}));
const dbPost = {
news_id: row.news_id,
date: row.date,
identifier: row.identifier,
type: row.type,
timestamp: row.timestamp,
headline: row.headline,
content: row.content,
url: row.url,
is_ai_summary: Boolean(row.is_ai_summary),
en_headline: row.en_headline,
en_content: row.en_content,
images,
};
if (lang === "en") {
if (dbPost.en_headline !== null) {
dbPost.headline = dbPost.en_headline;
}
if (dbPost.en_content !== null) {
dbPost.content = dbPost.en_content;
}
}
if (!dbPost.headline) {
dbPost.headline = dbPost.content;
}
return {
title: String(dbPost.headline || "Untitled"),
description: String(dbPost.content || "").slice(0, 300),
openGraph: {
title: String(dbPost.headline || "Untitled"),
description: String(dbPost.content || "").slice(0, 300),
images: dbPost.images?.[0]?.image
? [String(dbPost.images[0].image)]
: [],
},
};
} catch (dbErr) {
console.error("Database fallback error:", dbErr);
return { title: "Post not found" };
}
}
if (lang === "en") {
if (matchingPost.en_headline !== null) {
matchingPost.headline = matchingPost.en_headline;
}
if (matchingPost.en_content !== null) {
matchingPost.content = matchingPost.en_content;
}
}
if (!matchingPost.headline) {
matchingPost.headline = matchingPost.content;
}
return {
title: matchingPost.headline,
description: matchingPost.content.slice(0, 300),
openGraph: {
title: matchingPost.headline,
description: matchingPost.content.slice(0, 300),
images: matchingPost.images?.[0]?.image
? [matchingPost.images[0].image]
: [],
},
};
} catch (err) {
console.error(err);
return {
title: "Error loading post",
description: "There was a problem loading this news post.",
};
}
}
export default async function GamePage({
params,
searchParams,
}: {
params: Promise<{ gameName?: string }>;
searchParams: Promise<{ [key: string]: string | string[] | undefined }>;
}) {
const resolvedParams = await params;
const resolvedSearchParams = await searchParams;
const gameName = resolvedParams.gameName || "news";
const postId = resolvedSearchParams.post as string | undefined;
const lang = resolvedSearchParams.lang as string | undefined;
const mainNewsUrl = process.env.NEXT_PUBLIC_MAIN_NEWS_URL;
const apiUrlBase = process.env.NEXT_PUBLIC_API_URL;
if (postId) {
let newsPost: NewsData | null = null;
try {
let fetchUrl = `${apiUrlBase}/${gameName}_news.json`;
if (gameName === "news") {
fetchUrl = `${apiUrlBase}/news.json`;
}
const res = await fetch(fetchUrl);
if (res.ok) {
const data = await res.json();
const newsPosts = data.news_posts;
const matchingPost = newsPosts.find((news: any) => {
const contentHash =
news.content.split("").reduce((hash: number, char: string) => {
return (hash << 5) + hash + char.charCodeAt(0);
}, 5381) >>> 0;
const headlineHash =
(news.headline || "null")
.split("")
.reduce(
(hash: number, char: string) =>
(hash << 5) + hash + char.charCodeAt(0),
5381,
) >>> 0;
const newsId = `${news.identifier}-${news.timestamp}-${contentHash.toString(16)}-${headlineHash.toString(16)}`;
return newsId === postId;
});
if (matchingPost) {
newsPost = matchingPost;
}
}
// If not found in JSON, try database
if (!newsPost) {
const client = createClient({
url: process.env.SQLITE_DB!,
authToken: process.env.REMOTE_AUTH_TOKEN!,
});
const result = await client.execute({
sql: `SELECT
news_id, date, identifier, type, timestamp,
headline, content, url, is_ai_summary,
en_headline, en_content
FROM news
WHERE news_id = ?`,
args: [postId],
});
if (result.rows.length > 0) {
const row = result.rows[0];
// Get images for this news post
const imagesResult = await client.execute({
sql: `SELECT image_url, link_url FROM news_images WHERE news_id = ?`,
args: [postId],
});
const images = imagesResult.rows.map((img) => ({
image: img.image_url,
link: img.link_url,
}));
newsPost = {
identifier: row.identifier as string,
type: row.type as string | null,
timestamp: row.timestamp as number,
headline: row.headline as string | null,
content: row.content as string,
url: row.url as string | null,
is_ai_summary: Boolean(row.is_ai_summary),
en_headline: row.en_headline as string | null,
en_content: row.en_content as string | null,
images: images.map(img => ({
image: String(img.image),
link: img.link ? String(img.link) : null
})),
};
}
}
} catch (err) {
console.error("Error fetching news post:", err);
}
// If we found the post, render it
if (newsPost) {
return (
<NewsPostPage
newsPost={newsPost}
lang={lang}
gameName={gameName}
postId={postId}
mainNewsUrl={mainNewsUrl}
/>
);
}
}
// Default fallback page
const redirectUrl =
postId && mainNewsUrl
? gameName === "news"
? `${mainNewsUrl}/#${postId}`
: `${mainNewsUrl}/game/${gameName}#${postId}`
: mainNewsUrl;
return (
<main className="main">
<div className="content-wrapper">
<h1 className="title">573 UPDATES</h1>
<img
src={kairosImage.src}
alt="Updates image"
className="updates-image"
/>
{postId && !redirectUrl && (
<p style={{ color: "red", margin: "20px 0" }}>Post not found</p>
)}
{redirectUrl && (
<>
<br />
<a href={redirectUrl} className="redirect-link">
click here if not redirected
</a>
</>
)}
</div>
</main>
);
}
// Component to render a single news post
function NewsPostPage({
newsPost,
lang,
gameName,
postId,
mainNewsUrl,
}: {
newsPost: NewsData;
lang?: string;
gameName: string;
postId: string;
mainNewsUrl?: string;
}) {
let displayHeadline = newsPost.headline;
let displayContent = newsPost.content;
if (lang === "en") {
if (newsPost.en_headline !== null) {
displayHeadline = newsPost.en_headline;
}
if (newsPost.en_content !== null) {
displayContent = newsPost.en_content;
}
}
if (!displayHeadline) {
displayHeadline = displayContent;
}
const date = new Date(newsPost.timestamp * 1000).toLocaleDateString("ja-JP", {
year: "numeric",
month: "2-digit",
day: "2-digit",
});
const redirectUrl = mainNewsUrl
? gameName === "news"
? `${mainNewsUrl}/#${postId}`
: `${mainNewsUrl}/game/${gameName}#${postId}`
: null;
return (
<main className="min-h-screen text-white font-sans bg-black">
<div className="w-full max-w-xl mx-auto px-3 sm:px-4 py-5 box-border">
<div className="w-full bg-slate-800 border border-slate-700 rounded-lg shadow-xl shadow-black/30 overflow-hidden box-border">
{/* Post Header */}
<div className="p-3 border-b border-slate-600">
<div className="text-[13px] text-slate-400/80 mb-1.5">
{date}
</div>
{newsPost.type && (
<div className="inline-block text-[12px] italic text-slate-400 bg-slate-700 px-1.5 py-0.5 rounded">
{newsPost.type}
</div>
)}
</div>
{/* Content */}
<div className="p-3 min-h-[120px]">
{displayHeadline && (
<h2 className="font-bold text-base sm:text-lg mb-3 leading-snug text-slate-50 break-words">
{displayHeadline}
</h2>
)}
<div className="text-[13px] sm:text-sm whitespace-pre-line mb-3 leading-relaxed text-slate-200">
{displayContent
.split(/(\[.*?\]\(.*?\)|https?:\/\/[^\s]+)/g)
.map((part, idx) => {
const linkMatch = part.match(/\[(.*?)\]\((.*?)\)/);
const urlMatch = part.match(/https?:\/\/[^\s]+/);
if (linkMatch) {
return (
<Link
key={idx}
href={linkMatch[2]}
target="_blank"
rel="noopener noreferrer"
className="text-sky-400 underline decoration-blue-500 underline-offset-2 font-medium"
>
{linkMatch[1]}
</Link>
);
}
if (urlMatch) {
return (
<Link
key={idx}
href={urlMatch[0]}
target="_blank"
rel="noopener noreferrer"
className="text-sky-400 underline decoration-blue-500 underline-offset-2 font-medium"
>
{urlMatch[0]}
</Link>
);
}
return (
<span key={idx}>
{part}
</span>
);
})}
</div>
</div>
{/* AI Disclaimer */}
{newsPost.is_ai_summary && (
<div className="bg-slate-600 px-4 py-2.5 text-[12px] text-center text-slate-300">
This content was generated using AI and may contain inaccuracies
</div>
)}
{/* Machine Translation Disclaimer */}
{(newsPost.en_headline || newsPost.en_content) && lang === "en" && (
<div className="bg-slate-600 px-4 py-2.5 text-[12px] text-center text-slate-300">
This is a machine translation and may contain errors
</div>
)}
{/* Images */}
{newsPost.images && newsPost.images.length > 0 && (
<div className="w-full overflow-hidden">
<img
src={newsPost.images[0].image}
alt="News visual"
className="w-full h-auto max-h-[400px] object-contain block"
/>
</div>
)}
{/* Read More Link */}
{newsPost.url && (
<div className="bg-slate-600 px-4 py-3 text-center">
<Link
href={newsPost.url}
target="_blank"
rel="noopener noreferrer"
className="text-[15px] font-semibold text-sky-400 underline decoration-sky-400 underline-offset-2"
>
READ MORE
</Link>
</div>
)}
</div>
{/* About 573 UPDATES */}
<div className="mt-6 mb-4 p-3 text-center bg-slate-700 rounded-md border border-slate-600">
<h3 className="text-[15px] font-semibold mb-1.5 text-slate-50">
This is a perma-link hosted on 573 UPDATES
</h3>
<p className="text-[12px] text-slate-300 leading-tight">
A news aggregator for some arcade (and some not-so arcade) games.
Image data is loaded from external sources, and as such may not
always be available.
</p>
</div>
{/* Navigation Buttons */}
<div className="mt-3 flex flex-col items-center gap-2.5 text-center">
<Link
href="/"
className="block w-full max-w-xs bg-gradient-to-br from-blue-500 to-blue-700 text-white px-5 py-3.5 rounded-md text-sm font-semibold shadow-md shadow-blue-500/30 no-underline border-0 transition-all duration-200 text-center hover:brightness-110 active:translate-y-px"
>
Back to 573 UPDATES
</Link>
</div>
</div>
</main>
);
}
|